const app = getApp()

// printer信息
let services = []
let serviceId = 0
let writeCharacter = false
let readCharacter = false
let notifyCharacter = false
let isScanning = false

function preparePrint() {
    let deviceId = app.BLEInformation.deviceId
    if (deviceId != null && deviceId != '') {
        //断开重连后，需要重置这些参数，重新获取特征值
        services = []
        serviceId = 0
        writeCharacter = false
        readCharacter = false
        notifyCharacter = false
        //连接蓝牙并且打印配货单
        wx.closeBluetoothAdapter({
            success(res) {
                console.log(res)
            }
        })

        wx.openBluetoothAdapter({
            success: function (res) {
                wx.getBluetoothAdapterState({
                    success: function (res) {
                        console.log('openBluetoothAdapter success', res)
                        createBLEConnectionAndPrinter(deviceId)
                    },
                })
            }, fail: function () {
                wx.showModal({
                    title: '提示',
                    content: '蓝牙初始化失败，请到设置打开蓝牙',
                    showCancel: false
                })
            }
        })
    } else {
        wx.navigateTo({
            url: '/print-package/pages/conn/index'
        })
    }
}

// 连接蓝牙并打印
function createBLEConnectionAndPrinter(deviceId) {
    let _this = this
    wx.createBLEConnection({
        deviceId: deviceId,
        success: function (res) {
            console.log("已有deviceId值，连接蓝牙成功: %s", deviceId)
            getSeviceId()
        }, fail: function (e) {
            wx.showModal({
                title: '提示',
                content: '连接失败，请确定打印机是否正常运行?',
                showCancel: false
            })
            console.log(e)
            wx.hideLoading()
        }, complete: function (e) {
            console.log(e)
        }
    })
}

// 获取设备服务列表
function getSeviceId() {
    var that = this
    wx.getBLEDeviceServices({
        deviceId: app.BLEInformation.deviceId,
        success: function (res) {
            services = res.services
            getCharacteristics()
        }, fail: function (e) {
            console.log(e)
        }, complete: function (e) {
            console.log(e)
        }
    })
}

// 获取蓝牙低功耗设备某个服务中所有特征
function getCharacteristics() {
    var that = this
    var list = services
    var num = serviceId
    var write = writeCharacter
    var read = readCharacter
    var notify = notifyCharacter
    wx.getBLEDeviceCharacteristics({
        deviceId: app.BLEInformation.deviceId,
        serviceId: list[num].uuid,
        success: function (res) {
            for (var i = 0; i < res.characteristics.length; ++i) {
                var properties = res.characteristics[i].properties
                var item = res.characteristics[i].uuid
                if (!notify) {
                    if (properties.notify) {
                        app.BLEInformation.notifyCharaterId = item
                        app.BLEInformation.notifyServiceId = list[num].uuid
                        notify = true
                    }
                }
                if (!write) {
                    if (properties.write) {
                        app.BLEInformation.writeCharaterId = item
                        app.BLEInformation.writeServiceId = list[num].uuid
                        write = true
                    }
                }
                if (!read) {
                    if (properties.read) {
                        app.BLEInformation.readCharaterId = item
                        app.BLEInformation.readServiceId = list[num].uuid
                        read = true
                    }
                }
            }
            if (!write || !notify || !read) {
                num++
                writeCharacter = write
                readCharacter = read
                notifyCharacter = notify
                serviceId = num
                if (num == list.length) {
                    wx.showModal({
                        title: '提示',
                        content: '找不到该读写的特征值',
                        showCancel: false
                    })
                } else {
                    getCharacteristics()
                }
            } else {
                app.globalData.isConnBLE = true
                app.globalData.currentPageJs.printTheOrder()
            }
        }, fail: function (e) {
            console.log(e)
        }, complete: function (e) {
        }
    })
}

module.exports = {
    preparePrint: preparePrint
}
